package com.aistar.service.employer.impl;

import com.aistar.mapper.*;
import com.aistar.model.entity.Interview;
import com.aistar.service.employer.IInterviewService;
import com.aistar.util.DateUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName: InterviewService
 * @author: LiuWenXiu
 * @date: 2021/10/21 20:32
 * @Description: TODO...本类用来演示:
 */
@Service
@Transactional
public class InterviewServiceImpl implements IInterviewService {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private ResumeMapper resumeMapper;

    @Autowired
    private RecruitmentMapper recruitmentMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private InterviewMapper interviewMapper;

    @Autowired
    private DeliveryMapper deliveryMapper;

    @Autowired
    private RedisTemplate redisTemplate;


//    @Override
//    public void saveToRabbit(Interview interview) {
//        // 邀约面试的信息发送到rabbitmq中
//        rabbitTemplate.convertAndSend("hire","interview",interview);
//    }

    @Override
    public void saveToRabbit(Integer deliveryId, String interviewAddress, Date interviewTime) {
        // 首先获取简历投递id获取简历id和职位id
        Integer resumeId = deliveryMapper.findById(deliveryId).getResumeId();
//        System.out.println("获取的简历id："+resumeId);
        Integer recruitmentId = deliveryMapper.findById(deliveryId).getRecruitmentId();
        // 创建邀约面试实体类
        Interview interview = new Interview();
        interview.setDeliveryId(deliveryId);
        interview.setResumeId(resumeId);
        interview.setRecruitmentId(recruitmentId);
        interview.setInterviewAddress(interviewAddress);
        interview.setInterviewTime(interviewTime);
        // 放到队列中
        rabbitTemplate.convertAndSend("hire","interview",interview);
        rabbitTemplate.convertAndSend("hire","delivery",deliveryId);
    }

    @Override
    public void sendToEmail(Interview interview) {
        // 获取简历对应的邮箱
//        System.out.println("简历id："+interview.getResumeId());
        String email = resumeMapper.findById(interview.getResumeId()).getEmail();
//        System.out.println("邮箱："+email);
        // 获取邀约面试的公司名称
        Integer companyId = recruitmentMapper.findById(interview.getRecruitmentId()).getCompanyId();
        String companyName = companyMapper.findById(companyId).getName();

        // 发送邮件的过程
        SimpleMailMessage msg = new SimpleMailMessage();
        // 发送方
        msg.setFrom("2294014200@qq.com");
        // 设置标题
        msg.setSubject("轻聘-面试邀约");
        // 内容是验证码
        // 您好,xx公司邀请您于xx时间xx地点参加面试,详情内容可以登录轻聘查看
        String message = "您好," + companyName + "公司邀请您于" + DateUtil.format(interview.getInterviewTime(), "yyyy-MM-dd HH:mm:ss") + "在" + interview.getInterviewAddress() + "参加面试。\n";
        message += "更多详细内容请您登录轻聘官网进行查看，谢谢！";
        //接收方
        msg.setText(message);
        msg.setTo(email);
//        System.out.println("走到这里");
        // 真正的发送
        mailSender.send(msg);
    }

    @Override
    public int insertToInterview(Interview interview) {
        return interviewMapper.createInterview(interview);
    }

    @Override
    public int updateDelivery(Integer deliveryId) {
        return deliveryMapper.updateStatusById(deliveryId,"已邀约");
    }

    @Override
    public void saveDelToRabbit(Integer interviewId) {

//        // 首先根据邀约面试id获取简历投递id
//        Integer deliveryId = interviewMapper.findById(interviewId).getDeliveryId();
//        // 把数据放入redis中，key是邀约面试id，value是简历投递id
//        ValueOperations vop = redisTemplate.opsForValue();
//        // 放入数据
//        vop.set(interviewId.toString(),deliveryId.toString());
        // 把邀约面试id放到队列中
        rabbitTemplate.convertAndSend("hire","withdraw",interviewId);

    }

    @Override
    public void withDrawInterview(Integer interviewId) {
        //发送取消面试邮件，根据简历投递id修改投递状态为“有意向”，根据面试邀约id删除邀约信息
        // 发送取消面试邮件
        Interview interview = interviewMapper.findById(interviewId);
        // 获取简历对应的邮箱
//        System.out.println("简历id："+interview.getResumeId());
        String email = resumeMapper.findById(interview.getResumeId()).getEmail();
//        System.out.println("邮箱："+email);
        // 获取邀约面试的公司名称
        Integer companyId = recruitmentMapper.findById(interview.getRecruitmentId()).getCompanyId();
        String companyName = companyMapper.findById(companyId).getName();

        // 发送邮件的过程
        SimpleMailMessage msg = new SimpleMailMessage();
        // 发送方
        msg.setFrom("2294014200@qq.com");
        // 设置标题
        msg.setSubject("轻聘-面试取消提示");
        // 内容是验证码
        // 您好,xx公司邀请您于xx时间xx地点参加面试,详情内容可以登录轻聘查看
        String message = "您好," + companyName + "公司在" + interview.getInterviewAddress() + "的面试邀约已取消。\n";
        message += "更多详细内容请您登录轻聘官网进行查看，谢谢！";
        //接收方
        msg.setText(message);
        msg.setTo(email);
        // 真正的发送
        mailSender.send(msg);

        // 修改简历投递状态
        deliveryMapper.updateStatusById(interview.getDeliveryId(),"有意向");

        // 删除邀约信息
        interviewMapper.deleteById(interviewId);

    }


    @Override
    public void saveUpdateToRabbit(Interview interview) {
        // 更改数据库的数据
        interviewMapper.updateById(interview);
        rabbitTemplate.convertAndSend("hire","update",interview);
    }

    @Override
    public void updateInterview(Interview interview) {
        //// 更改面试邀约-> 发送修改的邮件，更改邀约面试表的信息
        // 发送修改事件地点信息
        // 获取简历对应的邮箱
//        System.out.println("简历id："+interview.getResumeId());
        String email = resumeMapper.findById(interview.getResumeId()).getEmail();
//        System.out.println("邮箱："+email);
        // 获取邀约面试的公司名称
        Integer companyId = recruitmentMapper.findById(interview.getRecruitmentId()).getCompanyId();
        String companyName = companyMapper.findById(companyId).getName();

        // 发送邮件的过程
        SimpleMailMessage msg = new SimpleMailMessage();
        // 发送方
        msg.setFrom("2294014200@qq.com");
        // 设置标题
        msg.setSubject("轻聘-面试更改提示");
        // 内容是验证码
        // 您好,xx公司邀请您于xx时间xx地点参加面试,详情内容可以登录轻聘查看
        String message = "您好," + companyName + "公司的面试邀约已更改。\n";
        message += "新的面试时间：" + DateUtil.format(interview.getInterviewTime(), "yyyy-MM-dd HH:mm:ss");
        message += "\n新的面试地点：" + interview.getInterviewAddress();
        message += "\n更多详细内容请您登录轻聘官网进行查看，谢谢！";
        //接收方
        msg.setText(message);
        msg.setTo(email);
        // 真正的发送
        mailSender.send(msg);


    }

    @Override
    public Interview getById(Integer id) {
        return interviewMapper.findById(id);
    }


}
